import * as Cesium from '@cesiumjs';
import * as g3xUtils from "@g3x/utils";
import * as mobx from 'mobx';

/* 7 */
function defaultOptionsOf(target) {
  var defaultOptions = {};
  const proto = Object.getPrototypeOf(target);
  while(proto){
    defaultOptions = { ...proto.constructor.defaultOptions, ...defaultOptions}
    proto = Object.getPrototypeOf(proto);
  }
  return defaultOptions;
}

function injectProps(target, options) {
  const defaultOptions = defaultOptionsOf(target)
  options = {
    ...defaultOptions,
    ...options
  }
  g3xUtils.g3x_defineReadonly(target, "defaultOptions", defaultOptions);
  g3xUtils.g3x_defineReadonly(target, "options", options);
  mobx.extendObservable(target, options)
}

/**
 * 所有需要读写的对象的基类
 * @description 从该基类派生的对象，具备读写能力。
 * @class
 */
class G3xObjBase {

  constructor(options) {
    injectProps(this, options)
    this._disposers = [];
  }

  get disposers() {
    return this._disposers
  }

  destroy() {
    this._isDestroying = true;
    this._disposers.reverse();
    this._disposers.forEach((d) => {
      return d();
    });
    this._disposers.length = 0;
    return Cesium.destroyObject(this);
  }
}

export default G3xObjBase;
